package grg.learn.compose.ui.ui.main

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewModelScope
import grg.learn.compose.ui.foundations.HLine
import grg.learn.compose.ui.models.MainListVM
import grg.learn.compose.ui.theme.Theme
import kotlinx.coroutines.launch


class MainActivity : ComponentActivity() {

    val theme = Theme.Dark

    val vm by viewModels<MainListVM>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Column(
                Modifier
                    .background(Color.Black)
            ) {

                /** 当前显示的tab **/
                var current by remember {
                    mutableStateOf(1)
                }

                /** 当前显示的tabStr **/
                var currentTabText by remember {
                    mutableStateOf("")
                }

                if (current != 3) {

                    /** 顶部toolbar **/
                    MainToolBar(
                        title = currentTabText,
                        theme = theme,
                        modifier = Modifier.height(50.dp),
                        left = {

                        }) {

                        /** 右边按钮 **/
                        MainToolBarMoreAction(Modifier.clickable {
                            Toast.makeText(this@MainActivity, "哈哈哈", Toast.LENGTH_SHORT).show()
                        })

                    }

                    HLine(lineColor = Color.White, lineHeight = .5f)
                }


                /** 首页主frame **/
                Box(
                    Modifier
                        .weight(1f)
                        .fillMaxWidth()
                ) {

                    when (current) {
                        0 -> {
                            Index(theme,vm)
                        }
                        1->{
                            FriendList(theme)
                        }
                        else -> {
                        }
                    }

                }

                /** 线 **/
                HLine(
                    lineColor = Color.White,
                    lineHeight = 0.5f
                )

                /** 底部菜单按钮 **/
                MainMenu(
                    theme.menuSelectColor,
                    theme.menuUnSelectColor,
                    current,
                    { position, menuData ->
                        current = position
                        currentTabText = menuData.text

                        if (current == 0){
                            println("===========")
                            vm.viewModelScope.launch {
                                println(vm.params.emit(100000))
                            }

                        }

                    }
                )

            }
        }
    }
}


